package com.libgdxbar.pvzlikegame.objects;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Shape2D;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool;
import com.libgdxbar.pvzlikegame.entities.MoveableEntity;
import com.libgdxbar.pvzlikegame.entities.RectMoveableCollisableEntity;

import org.libgdxbar.projects.interfaces.ICollision;
import org.libgdxbar.projects.interfaces.callback.CollisionCallback;
import org.libgdxbar.projects.interfaces.renderer.IShapeRender;

// 最简示例 僵尸类
// 继承 MoveableEntity 即可移动实体
// 实现 ShapeRenderable 接口 使用 ShapeRenderer 渲染图形
public class SampleShapeZombie extends RectMoveableCollisableEntity implements IShapeRender {
	int width = 50, height = 50;

	public SampleShapeZombie(float x, float y) {
		// 演示逻辑 创建随机方向移动的僵尸
		super(x, y, 50, 50);
		getMoveableEntity().setVelocity(-MathUtils.random(400f) + 200f, -MathUtils.random(400f) + 200f);
		setCollisionCallback(shape -> {
			Rectangle rect = (Rectangle) shape;
			MoveableEntity moveable = getMoveableEntity();
			if (rect.x > this.getRect().x) moveable.setVelocityX(-Math.abs(moveable.getVelocityX()));
			if (rect.x < this.getRect().x) moveable.setVelocityX(Math.abs(moveable.getVelocityX()));
			if (rect.y > this.getRect().y) moveable.setVelocityY(-Math.abs(moveable.getVelocityY()));
			if (rect.y < this.getRect().y) moveable.setVelocityY(Math.abs(moveable.getVelocityY()));
		});
	}

	// 实现渲染方法
	@Override
	public void render(ShapeRenderer renderer) {
		Vector2 position = getMoveableEntity().getPosition();
		renderer.setColor(Color.RED);
		renderer.rect(position.x, position.y, getWidth(), getHeight());
	}

	// 实现僵尸逻辑
	@Override
	public void run(float delta) {
		super.run(delta);
		MoveableEntity moveable = getMoveableEntity();
		Vector2 position = moveable.getPosition();
		// 这里碰屏幕边缘反弹只是演示逻辑而已 实际请自己实现
		if (position.x > 640 - width) moveable.setVelocityX(-Math.abs(moveable.getVelocityX()));
		if (position.x < 0) moveable.setVelocityX(Math.abs(moveable.getVelocityX()));
		if (position.y > 480 - height) moveable.setVelocityY(-Math.abs(moveable.getVelocityY()));
		if (position.y < 0) moveable.setVelocityY(Math.abs(moveable.getVelocityY()));
	}
}
